package com.wanxi.product.service;

import com.wanxi.product.core_sevice.*;
import com.wanxi.product.service.api.dto.*;
import com.wanxi.product.service.api.entity.SpuTag;
import com.wanxi.product.service.api.query.admin.TypeQuery;
import com.wanxi.product.service.api.query.enterprise.SpuQuery;
import com.wanxi.product.service.api.query.enterprise.SpuTagQuery;
import com.wanxi.product.service.api.query.user.SkuQuery;
import com.wanxi.product.service.api.result.CommonResult;
import com.wanxi.product.service.api.util.SkuStateEnum;
import com.wanxi.product.service.api.util.TypeEnum;
import com.wanxi.product.service.api.util.TypeStateEnum;
import com.wanxi.product.service.api.vo.user.SkuFindAllOutVo;
import com.wanxi.product.service.api.vo.user.SkuFindByIdOutVo;
import com.wanxi.product.service.api.feign.ProductUserService;
import com.wanxi.product.service.api.vo.user.TypeFindAllOutVo;
import com.wanxi.product.util.Check;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

@RestController
@Api(tags = "用户sku接口")
@RequestMapping("/user")
public class ProductUserServiceImpl implements ProductUserService {
    @Resource
    private SpuService spuService;

    @Resource
    private SkuService skuService;

    @Resource
    private Check check;

    @Override
    @ApiOperation("获取sku列表")
    @GetMapping("/sku/findAll")
    public CommonResult<List<SkuFindAllOutVo>> findAllSku(SkuQuery skuQuery) {
        /*符合条件的sku返回体列表*/
        List<SkuFindAllOutVo> skuFindAllOutVoList = new ArrayList<>();

        /*符合条件的spu id列表*/
        Set<Long> spuIdSet = new HashSet<>();
        /*符合条件的sku dto列表*/
        Set<SkuDto> skuDtoSet = new HashSet<>();

        /*通过分类id查找spu*/
        if (skuQuery.getTypeId() != null) {
            SpuQuery spuQuery = SpuQuery.builder()
                    .state(TypeStateEnum.启用.getCode())
                    .typeId(skuQuery.getTypeId())
                    .build();
            List<SpuDto> spuDtoList = spuService.findAllSpu(spuQuery);
            for (SpuDto spuDto : spuDtoList) {
                spuIdSet.add(spuDto.getSpuId());
            }
        }

        /*通过标签id查找spu id*/
        if (skuQuery.getTagId() != null) {
            SpuTagQuery spuTagQuery = SpuTagQuery.builder()
                    .tagId(skuQuery.getTagId())
                    .build();
            List<SpuTag> spuTagList = spuService.findAllSpuTag(spuTagQuery);
            for (SpuTag spuTag : spuTagList) {
                spuIdSet.add(spuTag.getSpuId());
            }
        }

        /*通过spu id查找sku*/
        for (Long spuId : spuIdSet) {
            SkuQueryDto skuQueryDto = SkuQueryDto.builder()
                    .spuId(spuId)
                    .state(SkuStateEnum.上架中.getCode())
                    .build();
            skuDtoSet.addAll(skuService.findAllSku(skuQueryDto));
        }

        /*根据商品名查找*/
        if (skuQuery.getData() != null && !skuQuery.getData().equals("")) {
            SkuQueryDto skuQueryDto2 = SkuQueryDto.builder()
                    .skuName(skuQuery.getData())
                    .state(SkuStateEnum.上架中.getCode())
                    .build();
            skuDtoSet.addAll(skuService.findAllSku(skuQueryDto2));
        }

        for (SkuDto skuDto : skuDtoSet) {
            PriceDto skuSalePriceDto = skuService.findPriceBySkuId(skuDto.getSkuId());

            /*id 名称 图片 销售价 市场价*/
            SkuFindAllOutVo skuFindAllOutVo = SkuFindAllOutVo.builder()
                    .skuId(skuDto.getSkuId())
                    .skuName(skuDto.getSkuName())
                    .img(skuDto.getImg())
                    .salePrice(skuSalePriceDto.getSalePrice())
                    .marketPrice(skuDto.getMarketPrice())
                    .build();
            skuFindAllOutVoList.add(skuFindAllOutVo);
        }

        return CommonResult.success(skuFindAllOutVoList);
    }

    @Override
    @ApiOperation("根据id查找sku")
    @ApiImplicitParam(name = "skuId", value = "要查询的商品sku的id", dataType = "Long")
    @GetMapping("/sku/findById")
    public CommonResult<SkuFindByIdOutVo> findSkuById(Long skuId) {
        SkuDto skuDto = skuService.findSkuBySkuId(skuId);

        /*id 名称 图片 市场价 规格 描述*/
        SkuFindByIdOutVo skuFindByIdOutVo = SkuFindByIdOutVo.builder()
                .skuId(skuDto.getSkuId())
                .skuName(skuDto.getSkuName())
                .img(skuDto.getImg())
                .marketPrice(skuDto.getMarketPrice())
                .specifications(skuDto.getSpecifications())
                .description(skuDto.getDescription())
                .build();

        /*销售价*/
        PriceDto skuSalePriceDto = skuService.findPriceBySkuId(skuId);
        skuFindByIdOutVo.setSalePrice(skuSalePriceDto.getSalePrice());

        /*通过spu id查找spu对应的标签id*/
        SpuTagQuery spuTagQuery = SpuTagQuery.builder()
                .spuId(skuDto.getSpuId())
                .build();
        List<SpuTag> spuTagList = spuService.findAllSpuTag(spuTagQuery);

        /*通过标签id查找标签，获取标签名称*/
        List<String> tagNameList = new ArrayList<>();
        for (SpuTag spuTag : spuTagList) {
            TagDto tagDto = spuService.findTagByTagId(spuTag.getTagId());
            tagNameList.add(tagDto.getTagName());
        }
        try {
            skuFindByIdOutVo.setTagName(tagNameList);
        }catch (Exception e){
            System.out.println("空");
        }


        return CommonResult.success(skuFindByIdOutVo);
    }

    @Override
    @ApiOperation("查找一级、二级或三级分类")
    @GetMapping("/type/findAll")
    public CommonResult<List<TypeFindAllOutVo>> findAllType(int typeCode) {
        List<TypeFindAllOutVo> typeVoList = new ArrayList<>();
        TypeQuery typeQuery = new TypeQuery();
        List<TypeDto> allType = spuService.findAllType(typeQuery);
        for (TypeDto typeDto : allType) {
            TypeEnum typeEnumTemp = check.checkTypeLevel(typeDto.getTypeId());
            if (typeEnumTemp.getCode()==typeCode) {
                TypeFindAllOutVo typeFindAllOutVo = TypeFindAllOutVo.builder()
                        .typeId(typeDto.getTypeId())
                        .typeName(typeDto.getTypeName())
                        .img(typeDto.getImg())
                        .description(typeDto.getDescription())
                        .build();
                if (typeCode == TypeEnum.三级分类.getCode()) {
                    TypeDto secondTypeDto = spuService.findTypeByTypeId(typeDto.getParentId());
                    TypeDto firstTypeDto = spuService.findTypeByTypeId(secondTypeDto.getParentId());
                    typeFindAllOutVo.setFirstParentTypeName(firstTypeDto.getTypeName());
                    typeFindAllOutVo.setSecondParentTypeName(secondTypeDto.getTypeName());
                }else if (typeCode == TypeEnum.二级分类.getCode()){
                    TypeDto firstTypeDto = spuService.findTypeByTypeId(typeDto.getParentId());
                    typeFindAllOutVo.setFirstParentTypeName((firstTypeDto.getTypeName()));
                }
                typeVoList.add(typeFindAllOutVo);
            }
        }
        return CommonResult.success(typeVoList);
    }


}
